home *** CD-ROM | disk | FTP | other *** search
- /* xlfio - xlisp file i/o */
-
- #ifdef CI_86
- #include "a:stdio.h"
- #include "xlisp.h"
- #endif
-
- #ifdef AZTEC
- #include "a:stdio.h"
- #include "xlisp.h"
- #endif
-
- #ifdef unix
- #include <stdio.h>
- #include <xlisp.h>
- #endif
-
-
- /* external variables */
-
- extern struct node *xlstack;
-
-
- /* local variables */
-
- static char buf[STRMAX+1];
-
-
- /**************************
- * xlfopen - open a file *
- **************************/
-
- static struct node *xlfopen(args)
- struct node *args;
- {
- struct node *oldstk,arg,fname,mode,*val;
- FILE *fp;
-
- oldstk = xlsave(&arg,&fname,&mode,NULL);
- arg.n_ptr = args;
-
- fname.n_ptr = xlevmatch(STR,&arg.n_ptr);
- mode.n_ptr = xlevmatch(STR,&arg.n_ptr);
-
- xllastarg(arg.n_ptr);
-
- if ((fp = fopen(fname.n_ptr->n_str,
- mode.n_ptr->n_str)) != NULL)
- {
- val = newnode(FPTR);
- val->n_fp = fp;
- }
- else
- val = NULL;
-
- xlstack = oldstk;
- return (val);
- }
-
-
- /****************************
- * xlfclose - close a file *
- ****************************/
-
- static struct node *xlfclose(args)
- struct node *args;
- {
- struct node *fptr;
-
- fptr = xlevmatch(FPTR,&args);
-
- xllastarg(args);
-
- if (fptr->n_fp == NULL)
- xlfail("file not open");
-
- fclose(fptr->n_fp);
- fptr->n_fp = NULL;
-
- return (NULL);
- }
-
-
- /*****************************************
- * xlgetc - get a character from a file *
- *****************************************/
-
- static struct node *xlgetc(args)
- struct node *args;
- {
- struct node *val;
- FILE *fp;
- int ch;
-
- if (args != NULL)
- fp = xlevmatch(FPTR,&args)->n_fp;
- else
- fp = stdin;
-
- xllastarg(args);
-
- if (fp == NULL)
- xlfail("file not open");
-
- if ((ch = getc(fp)) != EOF)
- {
- val = newnode(INT);
- val->n_int = ch;
- }
- else
- val = NULL;
-
- return (val);
- }
-
-
- /***************************************
- * xlputc - put a character to a file *
- ***************************************/
-
- static struct node *xlputc(args)
- struct node *args;
- {
- struct node *oldstk,arg,chr;
- FILE *fp;
-
- oldstk = xlsave(&arg,&chr,NULL);
- arg.n_ptr = args;
-
- chr.n_ptr = xlevmatch(INT,&arg.n_ptr);
-
- if (arg.n_ptr != NULL)
- fp = xlevmatch(FPTR,&arg.n_ptr)->n_fp;
- else
- fp = stdout;
-
- xllastarg(arg.n_ptr);
-
- if (fp == NULL)
- xlfail("file not open");
-
- putc(chr.n_ptr->n_int,fp);
-
- xlstack = oldstk;
- return (chr.n_ptr);
- }
-
-
- /***************************************
- * xlfgets - get a string from a file *
- ***************************************/
-
- static struct node *xlfgets(args)
- struct node *args;
- {
- struct node *str;
- char *sptr;
- FILE *fp;
-
- if (args != NULL)
- fp = xlevmatch(FPTR,&args)->n_fp;
- else
- fp = stdin;
-
- xllastarg(args);
-
- if (fp == NULL)
- xlfail("file not open");
-
- if (fgets(buf,STRMAX,fp) != NULL)
- {
- str = newnode(STR);
- str->n_str = strsave(buf);
-
- while (buf[strlen(buf)-1] != '\n')
- {
- if (fgets(buf,STRMAX,fp) == NULL)
- break;
- sptr = str->n_str;
- str->n_str = stralloc(strlen(sptr) + strlen(buf));
- strcpy(str->n_str,sptr);
- strcat(buf);
- strfree(sptr);
- }
- }
- else
- str = NULL;
-
- return (str);
- }
-
-
- /*************************************
- * xlfputs - put a string to a file *
- *************************************/
-
- static struct node *xlfputs(args)
- struct node *args;
- {
- struct node *oldstk,arg,str;
- FILE *fp;
-
- oldstk = xlsave(&arg,&str,NULL);
- arg.n_ptr = args;
-
- str.n_ptr = xlevmatch(STR,&arg.n_ptr);
-
- if (arg.n_ptr != NULL)
- fp = xlevmatch(FPTR,&arg.n_ptr)->n_fp;
- else
- fp = stdout;
-
- xllastarg(arg.n_ptr);
-
- if (fp == NULL)
- xlfail("file not open");
-
- fputs(str.n_ptr->n_str,fp);
-
- xlstack = oldstk;
- return (str.n_ptr);
- }
-
-
- /************************************
- * xlfinit - initialize file stuff *
- ************************************/
-
- xlfinit()
- {
- xlsubr("fopen",xlfopen);
- xlsubr("fclose",xlfclose);
- xlsubr("getc",xlgetc);
- xlsubr("putc",xlputc);
- xlsubr("fgets",xlfgets);
- xlsubr("fputs",xlfputs);
- }